//
//  GYMineBuyInfoDetailVC.m
//  GYDemo
//
//  Created by mac on 2018/3/27.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYMineBuyInfoDetailVC.h"
#import "Head.h"

@interface GYMineBuyInfoDetailVC ()<GCAdaptLabelDelegate>
@property (nonatomic,strong) GYMineBuyInfoDetailViewModel *buyInfoDetailViewModel;
@property (nonatomic,strong) UIView *headV;
@property (nonatomic,strong) GCAdaptLabel *nameLB;
@property (nonatomic,strong) GCAdaptLabel *typeLB;
@property (nonatomic,strong) UILabel *serverCountLB;
@property (nonatomic,strong) UILabel *priceLB;
@property (nonatomic,strong) UILabel *areaLB;
@property (nonatomic,strong) UILabel *countLB;
@property (nonatomic,strong) UILabel *endDateLB;
@property (nonatomic,strong) GCAdaptLabel *createDateLB;
@property (nonatomic,strong) UITextView *descriptionTV;
@end

@implementation GYMineBuyInfoDetailVC
#pragma mark - getter and setter
-(GYMineBuyInfoDetailViewModel *)buyInfoDetailViewModel
{
    if (!_buyInfoDetailViewModel) {
        _buyInfoDetailViewModel = [GYMineBuyInfoDetailViewModel new];
    }
    _buyInfoDetailViewModel.buyInfoModel = _buyInfoModel;
    return _buyInfoDetailViewModel;
}
-(UIView *)headV
{
    if (!_headV) {
        _headV = [UIView new];
        _headV.width = self.view.width;
        _headV.height = WFCGFloatY(339);
        _headV.backgroundColor = Main_Color_White;
        
        [_headV addSubview:self.nameLB];
        [_headV addSubview:self.typeLB];
        [_headV addSubview:self.priceLB];
        [_headV addSubview:self.areaLB];
        [_headV addSubview:self.countLB];
        [_headV addSubview:self.endDateLB];
        [_headV addSubview:self.serverCountLB];
        [_headV addSubview:self.createDateLB];
        
        UIView *sep1 = [UIView new];
        sep1.x = WFCGFloatX(13);
        sep1.y = WFCGFloatY(183.5);
        sep1.width = self.view.width - WFCGFloatX(26);
        sep1.height = 0.7f;
        sep1.backgroundColor = Main_Color_BG;
        [_headV addSubview:sep1];
        
        UILabel *needTitlelb = [UILabel new];
        needTitlelb.frame = WFCGRectMake(13, 200, 160, 21);
        needTitlelb.font = SYSTEMBOLDFONT(15);
        needTitlelb.textColor = Main_Color_Black;
        needTitlelb.text = @"需求描述";
        [_headV addSubview:needTitlelb];
        
        [_headV addSubview:self.descriptionTV];
        
        UIView *sep2 = [UIView new];
        sep2.x = 0;
        sep2.y = _headV.height - 5.0f;
        sep2.width = self.view.width;
        sep2.height = 5.0f;
        sep2.backgroundColor = Main_Color_BG;
        [_headV addSubview:sep2];
    }
    _typeLB.x = _nameLB.right + WFCGFloatX(5);
    return _headV;
}
-(GCAdaptLabel *)nameLB
{
    if (!_nameLB) {
        _nameLB = [[GCAdaptLabel alloc]initWithMaxWidth:WFCGFloatX(145)];
        _nameLB.font = SYSTEMBOLDFONT(21);
        _nameLB.textColor = Main_Color_Black;
        _nameLB.x = WFCGFloatX(13);
        _nameLB.y = WFCGFloatY(15);
        _nameLB.height = WFCGFloatY(30);
        _nameLB.delegate = self;
    }
    return _nameLB;
}
-(GCAdaptLabel *)typeLB
{
    if (!_typeLB) {
        _typeLB = [[GCAdaptLabel alloc]initWithMaxWidth:WFCGFloatX(60)];
        _typeLB.height = WFCGFloatY(14);
        _typeLB.x = _nameLB.right + WFCGFloatX(5);
        _typeLB.centerY = self.nameLB.centerY;
        _typeLB.font = SYSTEMFONT(10);
        _typeLB.textColor = Main_Color_Red;
        [WFFunctions WFUIaddbordertoView:_typeLB radius:WFCGFloatY(3) width:0.7f color:Main_Color_Red];
        _typeLB.delegate = self;
    }
    return _typeLB;
}
-(UILabel *)priceLB
{
    if (!_priceLB) {
        _priceLB = [[UILabel alloc]init];
        _priceLB.x = WFCGFloatX(13);
        _priceLB.y = WFCGFloatY(54.5);
        _priceLB.width = WFCGFloatX(300);
        _priceLB.height = WFCGFloatY(24);
        _priceLB.textColor = Main_Color_Yellow;
        _priceLB.font = SYSTEMFONT(17);
    }
    return _priceLB;
}
-(UILabel *)areaLB
{
    if (!_areaLB) {
        _areaLB = [[UILabel alloc]init];
        _areaLB.x = WFCGFloatX(13);
        _areaLB.y = WFCGFloatY(102);
        _areaLB.width = WFCGFloatX(200);
        _areaLB.height = WFCGFloatY(24);
        _areaLB.textColor = Main_Color_Gray;
        _areaLB.font = SYSTEMFONT(14);
    }
    return _areaLB;
}
-(UILabel *)countLB
{
    if (!_countLB) {
        _countLB = [[UILabel alloc]init];
        _countLB.x = WFCGFloatX(13);
        _countLB.y = WFCGFloatY(126);
        _countLB.width = WFCGFloatX(200);
        _countLB.height = WFCGFloatY(24);
        _countLB.textColor = Main_Color_Gray;
        _countLB.font = SYSTEMFONT(14);
    }
    return _countLB;
}
-(UILabel *)endDateLB
{
    if (!_endDateLB) {
        _endDateLB = [[UILabel alloc]init];
        _endDateLB.x = WFCGFloatX(13);
        _endDateLB.y = WFCGFloatY(150);
        _endDateLB.width = WFCGFloatX(160);
        _endDateLB.height = WFCGFloatY(24);
        _endDateLB.textColor = Main_Color_Gray;
        _endDateLB.font = SYSTEMFONT(14);
    }
    return _endDateLB;
}
-(UILabel *)serverCountLB
{
    if (!_serverCountLB) {
        _serverCountLB = [[UILabel alloc]init];
        _serverCountLB.x = self.view.width - WFCGFloatX(13) - WFCGFloatX(60);
        _serverCountLB.width = WFCGFloatX(60);
        _serverCountLB.height = WFCGFloatY(20);
        _serverCountLB.centerY = self.nameLB.centerY;
        _serverCountLB.textColor = Main_Color_Red;
        _serverCountLB.font = SYSTEMFONT(14);
        _serverCountLB.textAlignment = NSTextAlignmentRight;
    }
    return _serverCountLB;
}

-(GCAdaptLabel *)createDateLB
{
    if (!_createDateLB) {
        _createDateLB = [[GCAdaptLabel alloc]initWithMaxWidth:WFCGFloatX(80)];
        _createDateLB.x = WFCGFloatX(300);
        _createDateLB.centerY = self.endDateLB.centerY;
        _createDateLB.height = WFCGFloatY(15);
        _createDateLB.font = SYSTEMFONT(10);
        _createDateLB.textColor = Main_Color_Gray;
        [WFFunctions WFUIaddbordertoView:_createDateLB radius:WFCGFloatY(3) width:0.7f color:Main_Color_LightGray];
        _createDateLB.delegate = self;
    }
    return _createDateLB;
}
-(UITextView *)descriptionTV
{
    if (!_descriptionTV) {
        _descriptionTV = [UITextView new];
        _descriptionTV.x = WFCGFloatX(13);
        _descriptionTV.y = WFCGFloatY(235.5);
        _descriptionTV.width = self.view.width - WFCGFloatX(26);
        _descriptionTV.height = WFCGFloatY(80);
        _descriptionTV.textColor = Main_Color_Gray;
        _descriptionTV.font = SYSTEMFONT(14);
        _descriptionTV.editable = NO;
    }
    return _descriptionTV;
}
#pragma mark - event
#pragma mark - method
-(void)initilization
{
    self.navigationItem.title = @"求购信息详情";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.navigationController.navigationBar.shadowImage = [[UIImage new]rt_darkenWithLevel:0.3f];
    [self setNavgationTheme:1];
    [self.view addSubview:self.baseTable];
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.baseTable setSeparatorColor: Main_Color_BG];
    [self.baseTable setBackgroundColor:Main_Color_BG];
    [self.baseTable setTintColor:Main_Color_BG];
    self.baseTable.height = self.view.height - 64;
    self.baseTable.noDataBtn.hidden = YES;
    if (@available(iOS 11.0, *)) {
        //        self.baseTable.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
    }
    self.baseTable.mj_header.backgroundColor = Main_Color_Red;
    self.baseTable.scrollIndicatorInsets = self.baseTable.contentInset;
    [self.baseTable setTableHeaderView:self.headV];
    [self viewModelBand];
    if (_buyInfoModel.down != 1 && !_canNotEdit) {
        [self judgeValue];
    }
    
    @WeakSelf;
    self.baseTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [selfp reloadData];
    }];
    
   // [self reloadData];
}
-(void)viewModelBand
{
    @WeakSelf;
    GCBaseObservationModel *observation = [GCBaseObservationModel new];
    observation.observation = self.buyInfoDetailViewModel;
    observation.keyPath = @"buyInfoDetailModel";
    observation.handler = ^(NSString *keyPath) {
        [selfp loadHead];
    };
    [self registObservation:observation];
    
    GCBaseObservationModel *observation2 = [GCBaseObservationModel new];
    observation2.observation = self.buyInfoDetailViewModel;
    observation2.keyPath = @"serverDataSource";
    observation2.handler = ^(NSString *keyPath) {
        [selfp reloadView];
    };
    [self registObservation:observation2];
}
-(void)reloadView
{
    [self.baseTable reloadData];
    [self.baseTable setTableHeaderView:self.headV];
}
-(void)reloadData
{
    [self.buyInfoDetailViewModel requestDetail];
}

- (void)judgeValue {
    
    @WeakSelf;
    [self setRightBarButtonWithTitle:@"修改" titleColor:Main_Color_Red withBlock:^() {
        GYMineBuyInfoEditVC *edit = [GYMineBuyInfoEditVC new];
        edit.detailVC = selfp;
        edit.listVC = selfp.listVC;
        [edit.buyInfoEditViewModel setBuyInfoDetailModel:selfp.buyInfoDetailViewModel.buyInfoDetailModel];
        [selfp.navigationController pushViewController:edit animated:YES];
    }];
}
-(void)loadHead
{
    _nameLB.text = _buyInfoDetailViewModel.name;
    _typeLB.text = [NSString stringWithFormat:@" %@ ",_buyInfoDetailViewModel.type];
    _priceLB.text = _buyInfoDetailViewModel.price;
    NSString *prices = [NSString stringWithFormat:@"地区：%@",_buyInfoDetailViewModel.area];
    NSMutableAttributedString *mprices = [[NSMutableAttributedString alloc]initWithString:prices];
    [mprices addAttribute:NSForegroundColorAttributeName value:Main_Color_LightGray range:[prices rangeOfString:@"地区："]];
    _areaLB.attributedText = mprices;
    
    NSString *counts = [NSString stringWithFormat:@"数量：%@",_buyInfoDetailViewModel.count];
    NSMutableAttributedString *mcounts = [[NSMutableAttributedString alloc]initWithString:counts];
    [mcounts addAttribute:NSForegroundColorAttributeName value:Main_Color_LightGray range:[counts rangeOfString:@"数量："]];
    _countLB.attributedText = mcounts;
    
    NSString *enddates = [NSString stringWithFormat:@"到期时间：%@",_buyInfoDetailViewModel.endDate];
    NSMutableAttributedString *menddates = [[NSMutableAttributedString alloc]initWithString:enddates];
    [menddates addAttribute:NSForegroundColorAttributeName value:Main_Color_LightGray range:[enddates rangeOfString:@"到期时间："]];
    _endDateLB.attributedText = menddates;
    _serverCountLB.text = _buyInfoDetailViewModel.serverCount;
    _createDateLB.text = [NSString stringWithFormat:@" %@ ",_buyInfoDetailViewModel.createDate];
    _descriptionTV.text = _buyInfoDetailViewModel.descriptions;
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initilization];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - delegate
- (void)callback:(id)sender {
    [self performSelectorOnMainThread:@selector(reloadView) withObject:nil waitUntilDone:YES];
}
#pragma mark - table delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.buyInfoDetailViewModel.serverDataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellID = @"CellID";
    GYMineLikeProductCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[GYMineLikeProductCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.deleteBtn.hidden = YES;
    }
    GYMineLikeProductInfoModel *model = self.buyInfoDetailViewModel.serverDataSource[indexPath.row];
    cell.model = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = [GYMineLikeProductCell finalHeight];
    return height;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//    if (_buyInfoDetailViewModel.serverDataSource.count > 0) {
//        return WFCGFloatY(24.5);
//    }else{
//        return 0.001f;
//    }
    return WFCGFloatY(37.5);
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.backgroundColor = Main_Color_White;
    
    UIView *sign = [UIView new];
    sign.backgroundColor = Main_Color_Red;
    sign.frame = WFCGRectMake(13, 16.5, 5, 15);
    [view addSubview:sign];
    
    UILabel *titlelb = [UILabel new];
    titlelb.frame = WFCGRectMake(28, 13.5, 160, 21);
    titlelb.font = SYSTEMFONT(15);
    titlelb.textColor = Main_Color_Black;
    titlelb.text = @"供货列表";
    [view addSubview:titlelb];
    
//    if (_buyInfoDetailViewModel.serverDataSource.count > 0) {
//        UIView *sign = [UIView new];
//        sign.backgroundColor = Main_Color_Red;
//        sign.frame = WFCGRectMake(13, 16.5, 5, 15);
//        [view addSubview:sign];
//
//        UILabel *titlelb = [UILabel new];
//        titlelb.frame = WFCGRectMake(28, 13.5, 160, 21);
//        titlelb.font = SYSTEMFONT(15);
//        titlelb.textColor = Main_Color_Black;
//        titlelb.text = @"供货列表";
//        [view addSubview:titlelb];
//    }else{
//    }
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GYMineLikeProductInfoModel *model = self.buyInfoDetailViewModel.serverDataSource[indexPath.row];
    GYHomeSaleDetailVC *detail = [[GYHomeSaleDetailVC alloc] init];
    detail.passID = [NSString stringWithFormat:@"%@",model.id];
    [self.navigationController pushViewController:detail animated:YES];
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
